//
//  HomePageVC.m
//  WalletAPP
//
//  Created by 罗弟祥 on 2019/10/4.
//  Copyright © 2019 罗弟祥. All rights reserved.
//

#import "HomePageVC.h"
#import "GetView.h"
#import "BuyView.h"
#import "HomePageCell1.h"
#import "HomePageCell2.h"
#import "HashrateVC.h"
#import "LoginVC.h"
@interface HomePageVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, copy) BuyView *buyView;
@property (nonatomic, copy) GetView *getView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *view_h;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UIButton *btn;

@property (nonatomic, retain) NSArray *icoImgArr;
@property (nonatomic, retain) UIView *popupView;

// 屏幕高度与宽度
#define screenW  [UIScreen mainScreen].bounds.size.width
#define screenH  [UIScreen mainScreen].bounds.size.height


@end

@implementation HomePageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    self.view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([HomePageVC class]) owner:self options:nil].firstObject;
    self.tableView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    self.tableView.estimatedRowHeight = 2;
    
    _btn.layer.cornerRadius = 20;
    
    self.icoImgArr = @[@"icon_home_item1", @"icon_home_item2", @"icon_home_item3"];
}
- (IBAction)nowBtnClick:(UIButton *)sender {
    
}

- (IBAction)buyBtnClick:(UIButton *)sender {
    [self setBuyViewBlock];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.icoImgArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        HomePageCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([HomePageCell2 class]) owner:self options:nil].firstObject;
        }
        cell.icoImgV.image = [UIImage imageNamed:self.icoImgArr[indexPath.row]];
        
        return cell;
    }
    
    HomePageCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([HomePageCell1 class]) owner:self options:nil].firstObject;
    }
    
    cell.icoImgV.image = [UIImage imageNamed:self.icoImgArr[indexPath.row]];
    NSArray *typeImgArr = @[@"icon_home_item11", @"icon_home_item21", @""];
    cell.typeImgV.image = [UIImage imageNamed:typeImgArr[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self setBuyViewBlock];
    }else if (indexPath.row == 1){
        [self setGetViewBlock];

    }else{
        HashrateVC *vc = [HashrateVC new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
}


-(void)setBuyViewBlock{
    [self setViewSubview:YES];
    
    [self.view addSubview:self.popupView];
    self.popupView.frame = CGRectMake(0, 0, screenW, screenH);

    [self.popupView addSubview:self.buyView];
    [self.buyView setCellBlock:^(NSInteger index) {
        if (index != 1) {
            [self setViewSubview:NO];
            self.popupView.frame = CGRectMake(0, screenH, screenW, screenH);
            [UIView animateWithDuration:0.3 animations:^{
                self.buyView.frame = CGRectMake(0, screenH, screenW, 400);
                [self.view setNeedsLayout];
            }];
            
            self.view_h.constant = 300;
            [UIView animateWithDuration:0.3 animations:^{
                [self.view layoutIfNeeded];
                [self.view setNeedsLayout];
            } completion:^(BOOL finished) {
                //            [self shakeAnimationForView:self.view];
                
            }];
            self.buyView.scrollView.contentOffset = CGPointMake(0, 0);

        }else{
            [UIView animateWithDuration:0.5 animations:^{
                self.buyView.scrollView.contentOffset = CGPointMake(screenW, 0);
            }];
        }
    }];
    
    self.buyView.frame = CGRectMake(0, screenH, screenW, 500);
    
    [UIView animateWithDuration:0.6 animations:^{
        self.buyView.frame = CGRectMake(0, screenH - 515, screenW, 500);
        [self.view layoutIfNeeded];
        [self.view setNeedsLayout];
    } completion:^(BOOL finished) {
        //        [self shakeAnimationForView:self.hView];
        self.buyView.frame = CGRectMake(0, screenH - 500, screenW, 500);
        [UIView animateWithDuration:0.3 animations:^{
            [self.view layoutIfNeeded];
            [self.view setNeedsLayout];
        }];
    }];
    
    
    
    self.view_h.constant = 110;
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
        [self.view setNeedsLayout];
    }];
    
}

-(void)setGetViewBlock{
    [self setViewSubview:YES];
    
    
    [self.view addSubview:self.getView];
    [self.getView setCellBlock:^{
        [self setViewSubview:NO];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.getView.frame = CGRectMake(0, screenH, screenW, 400);
            [self.view setNeedsLayout];
        }];
        
        self.view_h.constant = 300;
        [UIView animateWithDuration:0.3 animations:^{
            [self.view layoutIfNeeded];
            [self.view setNeedsLayout];
        } completion:^(BOOL finished) {
            
        }];
    }];
    self.getView.frame = CGRectMake(0, screenH, screenW, 500);
    
    [UIView animateWithDuration:0.6 animations:^{
        self.getView.frame = CGRectMake(0, screenH - 515, screenW, 500);
        [self.view layoutIfNeeded];
        [self.view setNeedsLayout];
    } completion:^(BOOL finished) {
        //        [self shakeAnimationForView:self.hView];
        self.getView.frame = CGRectMake(0, screenH - 500, screenW, 500);
        [UIView animateWithDuration:0.3 animations:^{
            [self.view layoutIfNeeded];
            [self.view setNeedsLayout];
        }];
    }];
    
    
    
    self.view_h.constant = 110;
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
        [self.view setNeedsLayout];
    }];
    
}

-(void)setViewSubview:(BOOL)h{
    for (UIView *view in self.headView.subviews) {
        view.hidden = h;
    }
}

-(BuyView *)buyView{
    if (!_buyView) {
        _buyView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([BuyView class]) owner:self options:nil].firstObject;
        
    }
    return _buyView;
}
-(GetView *)getView{
    if (!_getView) {
        _getView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([GetView class]) owner:self options:nil].firstObject;
        
    }
    return _getView;
}
-(UIView *)popupView{
    if (!_popupView) {
        _popupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
        _popupView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    }
    return _popupView;
}
/**
 *  抖动效果
 *  @param view 要抖动的view
 */

- (void)shakeAnimationForView:(UIView *) view {
    
    CALayer *viewLayer = view.layer;
    
    CGPoint position = viewLayer.position;
    
    CGPoint x = CGPointMake(position.x, position.y + 10);
    
    CGPoint y = CGPointMake(position.x, position.y - 10);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    
    [animation setAutoreverses:YES];
    
    [animation setDuration:.06];
    
    [animation setRepeatCount:1];
    
    [viewLayer addAnimation:animation forKey:nil];
    
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
